package com.inossem.warehousestorage_inossem.adapter.inStorage;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.in_storage.InStorageReceiveMaterielDetailActivity;
import com.inossem.warehousestorage_inossem.activity.in_storage.InStorageReceiveOverviewActivity;
import com.inossem.warehousestorage_inossem.activity.in_storage.extra.InStorageExtra;
import com.inossem.warehousestorage_inossem.bean.inStorage.InStorageReceiveOverviewBean;
import com.inossem.warehousestorage_inossem.utils.InStorageUtils;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.List;

import static com.inossem.warehousestorage_inossem.R.id.inStorageNumber;
import static com.inossem.warehousestorage_inossem.R.id.serialNumber;

/**
 * Created by leij on 2017/6/6.
 */

public class ReceiveInStorageItemListAdapter extends BaseAdapter {

    private Context mContext;
    private List<InStorageReceiveOverviewBean.DataBean.ReceiveInStorageListBean> mReceiveInStorageItemList;
    private LayoutInflater inflater;
    private InStorageReceiveOverviewActivity.InStorageReceiveOverviewListener mInStorageReceiveListener;
    private String mMaterielCode, mPurchaseOrderNumber, mSupplier, mCreateDate, mOrderType, mPurchaseOrganization, mPurchaseGroup, mContractCode;

    public ReceiveInStorageItemListAdapter(Context context, InStorageReceiveOverviewBean.DataBean data, List<InStorageReceiveOverviewBean.DataBean.ReceiveInStorageListBean> inStorageReceiveItemList, String materielCode, InStorageReceiveOverviewActivity.InStorageReceiveOverviewListener inStorageReceiveOverviewListener) {
        mContext = context;
        mPurchaseOrderNumber = data.getPurchase();
        mSupplier = data.getSupplier();
        mCreateDate = data.getCreateDate();
        mOrderType = data.getOrderType();
        mPurchaseOrganization = data.getPurchaseOrganizationCode() + " - " + data.getPurchaseOrganizationValue();
        mPurchaseGroup = data.getPurchaseGroupCode() + " - " + data.getPurchaseGroupValue();
        mContractCode = data.getContractCode();
        mReceiveInStorageItemList = inStorageReceiveItemList;
        mInStorageReceiveListener = inStorageReceiveOverviewListener;
        inflater = LayoutInflater.from(context);
        mMaterielCode = materielCode;
    }

    @Override
    public int getCount() {
        return mReceiveInStorageItemList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mReceiveInStorageItemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_in_storage_receive_item_list_first_item, null);
            TextView purchaseOrderNumber = (TextView) convertView.findViewById(R.id.purchaseOrderNumber);
            TextView supplier = (TextView) convertView.findViewById(R.id.supplier);
            TextView createDate = (TextView) convertView.findViewById(R.id.createDate);
            TextView orderType = (TextView) convertView.findViewById(R.id.orderType);
            TextView purchaseOrganization = (TextView) convertView.findViewById(R.id.purchaseOrganization);
            TextView purchaseGroup = (TextView) convertView.findViewById(R.id.purchaseGroup);
            TextView contractCode = (TextView) convertView.findViewById(R.id.contractCode);
            TextView showAndHide = (TextView) convertView.findViewById(R.id.showAndHide);
            PercentRelativeLayout hideLayout = (PercentRelativeLayout) convertView.findViewById(R.id.hideLayout);
            EditText materielCode = (EditText) convertView.findViewById(R.id.materielCode);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            purchaseOrderNumber.setText(mPurchaseOrderNumber);
            supplier.setText(mSupplier);
            createDate.setText(mCreateDate);
            orderType.setText(mOrderType);
            purchaseOrganization.setText(mPurchaseOrganization);
            purchaseGroup.setText(mPurchaseGroup);
            contractCode.setText(mContractCode);
            materielCode.setText(mMaterielCode);
            showAndHide.setOnClickListener((View v) -> {
                if (hideLayout.getVisibility() == View.GONE) {
                    hideLayout.setVisibility(View.VISIBLE);
                    ((TextView) v).setText(mContext.getResources().getString(R.string.activity_receive_delivery_overview_hide_information));
                } else if (hideLayout.getVisibility() == View.VISIBLE) {
                    hideLayout.setVisibility(View.GONE);
                    ((TextView) v).setText(mContext.getResources().getString(R.string.activity_receive_delivery_overview_show_information));
                }
            });
//            materielCode.addTextChangedListener(new QRCodeEditTextWatcher(materielCode) {
//                @Override
//                public void onScan(String text) {
//                    Utils.closeSoftKeyboard(mContext, materielCode);
//                    mInStorageReceiveListener.onNoDoubleClickListener(text);
//                }
//            });
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Utils.closeSoftKeyboard(mContext, v);
                    mInStorageReceiveListener.onNoDoubleClickListener(materielCode.getText().toString());
                }
            });
        } else {
            InStorageReceiveOverviewBean.DataBean.ReceiveInStorageListBean receiveInstorageItem = mReceiveInStorageItemList.get(position - 1);
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_in_storage_receive_item_list, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, receiveInstorageItem);
        }
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.serialNumber = (TextView) view.findViewById(serialNumber);
        viewHolder.materielCode = (TextView) view.findViewById(R.id.materielCode);
        viewHolder.materielDescribe = (TextView) view.findViewById(R.id.materielDescribe);
        viewHolder.inStorageNumber = (TextView) view.findViewById(inStorageNumber);
        viewHolder.inventory = (TextView) view.findViewById(R.id.inventory);
        viewHolder.batch = (TextView) view.findViewById(R.id.batch);
        viewHolder.batchLayout = (PercentRelativeLayout) view.findViewById(R.id.batchLayout);
        viewHolder.headLayout = (PercentRelativeLayout) view.findViewById(R.id.headLayout);
        viewHolder.contentLayout = (PercentRelativeLayout) view.findViewById(R.id.contentLayout);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, InStorageReceiveOverviewBean.DataBean.ReceiveInStorageListBean receiveInStorageItem) {
        viewHolder.serialNumber.setText(receiveInStorageItem.getSerial());
        viewHolder.materielCode.setText(receiveInStorageItem.getMaterielCode());
        viewHolder.materielDescribe.setText(receiveInStorageItem.getMaterielDescription());
        viewHolder.inStorageNumber.setText("入库" + (TextUtils.isEmpty(receiveInStorageItem.getRealInStorageNumber()) ? receiveInStorageItem.getInStorageNumber() : receiveInStorageItem.getRealInStorageNumber()) + receiveInStorageItem.getUnit());
        viewHolder.inventory.setText(receiveInStorageItem.getInventoryAddressCode() + " - " + receiveInStorageItem.getInventoryAddressValue());
        int flag = InStorageUtils.hasReceiveBatchInfo(receiveInStorageItem.getBatch());
        switch (flag) {
            case InStorageUtils.NO_BATCH:
                viewHolder.batchLayout.setVisibility(View.GONE);
                break;
            case InStorageUtils.BATCH_NO_MESSAGE:
                break;
            case InStorageUtils.BATCH_HAS_MESSAGE:
                viewHolder.batchLayout.setVisibility(View.VISIBLE);
                viewHolder.batch.setText("修改");
                break;
            case InStorageUtils.BATCH_NO_MESSAGE_BUT_REQUIRED:
                viewHolder.batchLayout.setVisibility(View.VISIBLE);
                viewHolder.batch.setText("必填");
                break;
        }
        if (InStorageUtils.checkReceiveItem(receiveInStorageItem)) {
            viewHolder.headLayout.setBackgroundResource(R.color.test_green);
        } else {
            viewHolder.headLayout.setBackgroundResource(R.color.view_receive_item_list_dark_row);
        }
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent = new Intent(context, InStorageReceiveMaterielDetailActivity.class);
                String serial = viewHolder.serialNumber.getText().toString();
                intent.putExtra(InStorageExtra.SERIAL_NUMBER, serial);
                context.startActivity(intent);
            }
        });
    }

    private class ViewHolder {
        private TextView serialNumber, materielCode, materielDescribe, inStorageNumber, inventory, batch;

        private PercentRelativeLayout batchLayout, headLayout, contentLayout;
    }
}
